# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# Name:        ervo
# Purpose:
#
# Author:      Yegor Chemisov
#
# Created:     29.04.2011
# Copyright:   (c) Yegor Chemisov 2011
# Licence:     GNU GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import configparser
import textparser
import dictparser
import re
import os

REVISION        = re.search(r'\d+','$Rev: 152 $').group()
EORU_DATE       = '2011-09-12'

def bok_mobi():
    bk = dictparser.DictionaryParser()
    parser = textparser.TextParser('eoru2mobi.ini')
    bk.templates = parser.config['Templates']
    parser.carryOut('Dictionary', {'formatword': bk.formatWord})
    #parser.carryOut('Index')
    #parser.carryOut('Articles')

    output_dir = parser.config['DEFAULT']['Output directory']
    template_dir = parser.config['DEFAULT']['Templates directory']
    open(output_dir + "eoru.opf", 'w', encoding='utf8').write(
        open(template_dir+ '/eoru.opf', 'r', encoding='utf8').read() % EORU_DATE)

    # Вызываем программу kindlegen для генерации словаря
    print ("\nGenerating MOBI file")
    cmd = 'C:/KindleGen/kindlegen.exe ' + \
            output_dir + 'eoru.opf -verbose -c1 ' \
            '-o eoru-%s-r%s.mobi > log.txt' % (EORU_DATE, REVISION)
    # os.system(cmd)
    open("ervo.bat", 'w').write(cmd)
        
def main():
    bok_mobi()

if __name__ == '__main__':
    main()
